/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace EXtremecode.Debugging
{
	/// <summary>
	/// Summary description for FrmExpression.
	/// </summary>
	public class FrmExpression : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ImageList imageList;
        private TreeView trv;
        private Splitter splitter1;
        private SplitContainer splitContainer1;
        private TextBox txtExpression;
        private TextBox txtInputString;
        private Panel panel1;
        private Button btn;
		private System.ComponentModel.IContainer components;

		public FrmExpression()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			txtInputString.Text=txtExpression.Text=string.Empty;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
			
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmExpression));
            this.imageList = new System.Windows.Forms.ImageList(this.components);
            this.trv = new System.Windows.Forms.TreeView();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.txtExpression = new System.Windows.Forms.TextBox();
            this.txtInputString = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btn = new System.Windows.Forms.Button();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // imageList
            // 
            this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
            this.imageList.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList.Images.SetKeyName(0, "");
            this.imageList.Images.SetKeyName(1, "");
            // 
            // trv
            // 
            this.trv.Dock = System.Windows.Forms.DockStyle.Left;
            this.trv.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.trv.ImageIndex = 0;
            this.trv.ImageList = this.imageList;
            this.trv.Location = new System.Drawing.Point(0, 0);
            this.trv.Name = "trv";
            this.trv.SelectedImageIndex = 0;
            this.trv.Size = new System.Drawing.Size(152, 310);
            this.trv.TabIndex = 1;
            this.trv.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.trv_AfterSelect);
            // 
            // splitter1
            // 
            this.splitter1.Location = new System.Drawing.Point(152, 0);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(3, 310);
            this.splitter1.TabIndex = 2;
            this.splitter1.TabStop = false;
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(155, 0);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.btn);
            this.splitContainer1.Panel1.Controls.Add(this.txtExpression);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.txtInputString);
            this.splitContainer1.Size = new System.Drawing.Size(453, 310);
            this.splitContainer1.SplitterDistance = 141;
            this.splitContainer1.TabIndex = 3;
            // 
            // txtExpression
            // 
            this.txtExpression.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtExpression.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtExpression.Location = new System.Drawing.Point(0, 0);
            this.txtExpression.Multiline = true;
            this.txtExpression.Name = "txtExpression";
            this.txtExpression.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtExpression.Size = new System.Drawing.Size(428, 138);
            this.txtExpression.TabIndex = 0;
            this.txtExpression.Text = "textBox1";
            // 
            // txtInputString
            // 
            this.txtInputString.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtInputString.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtInputString.HideSelection = false;
            this.txtInputString.Location = new System.Drawing.Point(0, 0);
            this.txtInputString.Multiline = true;
            this.txtInputString.Name = "txtInputString";
            this.txtInputString.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtInputString.Size = new System.Drawing.Size(453, 165);
            this.txtInputString.TabIndex = 1;
            this.txtInputString.Text = "text box";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.splitContainer1);
            this.panel1.Controls.Add(this.splitter1);
            this.panel1.Controls.Add(this.trv);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(608, 310);
            this.panel1.TabIndex = 1;
            // 
            // btn
            // 
            this.btn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btn.Location = new System.Drawing.Point(434, 3);
            this.btn.Name = "btn";
            this.btn.Size = new System.Drawing.Size(16, 22);
            this.btn.TabIndex = 2;
            this.btn.Text = ">";
            this.btn.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.btn.Click += new System.EventHandler(this.btn_Click);
            // 
            // FrmExpression
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(608, 310);
            this.Controls.Add(this.panel1);
            this.Name = "FrmExpression";
            this.Text = "FrmExpression";
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel1.PerformLayout();
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.Panel2.PerformLayout();
            this.splitContainer1.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main(string[] args)
		{
			Application.Run(new FrmExpression());
		}

		public void btn_Click(object sender, System.EventArgs e)
		{
			//Build Tree With Groups
			trv.Nodes.Clear();
			try
			{
				Regex regex=new Regex(txtExpression.Text,RegexOptions.Multiline | RegexOptions.IgnoreCase);
				
				foreach(Match match in regex.Matches(txtInputString.Text))
				{
					TreeNode matchNode=new TreeNode(match.Value,0,0);
					matchNode.Tag=match;
					trv.Nodes.Add(matchNode);

					foreach(string groupName in regex.GetGroupNames())
					{
						Group group=match.Groups[groupName]; 
						TreeNode groupNode=new TreeNode(
							string.Format("{0}: {1}",groupName,group.Value),1,1);
						groupNode.Tag=group;
						matchNode.Nodes.Add(groupNode);
					}
				}
			}
			catch
			{
				//Ignore
			}

		}

		private void trv_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			TreeNode node = e.Node;
			Capture capture= node.Tag as Capture;
			txtInputString.Select(capture.Index,capture.Value.Length); 
			txtInputString.ScrollToCaret();

		}

		


		public string Expression
		{
			set
			{
				txtExpression.Text=value;
				
			}
			get
			{
				return txtExpression.Text;
			}
		}

		public object InputString
		{
			set
			{
				txtInputString.Text=value.ToString();
				
			}
			get
			{
				return txtInputString.Text;
			}
		}
		
	}
}
